/**
 * Copyright (c) 2022 aoshiguchen
 * <p>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p>
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * <p>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.sl.proxy.server.controller;


import com.sl.proxy.server.base.page.PageInfo;
import com.sl.proxy.server.base.page.PageQuery;
import com.sl.proxy.server.controller.req.system.*;
import com.sl.proxy.server.controller.res.system.*;
import com.sl.proxy.server.service.PortPoolService;
import com.sl.proxy.server.util.ParamCheckUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 端口池
 * @author: aoshiguchen
 * @date: 2022/8/7
 */
@RequestMapping("/port-pool")
@RestController
@RequiredArgsConstructor
public class PortPoolController {
    private final PortPoolService portPoolService;

    @GetMapping("/page")
    public PageInfo<PortPoolListRes> page(PageQuery pageQuery, PortPoolListReq req) {
        ParamCheckUtil.checkNotNull(pageQuery, "pageQuery");
        return portPoolService.page(pageQuery, req);
    }

    @PostMapping("/update")
    public PortPoolUpdateRes update(@RequestBody PortPoolUpdateReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");
        ParamCheckUtil.checkNotNull(req.getGroupId(), "groupId");
        return portPoolService.update(req);
    }

    @GetMapping("/list")
    public List<PortPoolListRes> list(PortPoolListReq req) {
        return portPoolService.list(req);
    }

    @PostMapping("/create")
    public PortPoolCreateRes create(@RequestBody PortPoolCreateReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotEmpty(req.getPort(), "port");
        ParamCheckUtil.checkNotNull(req.getGroupId(), "groupId");

        return portPoolService.create(req);
    }

    @PostMapping("/update/enable-status")
    public PortPoolUpdateEnableStatusRes updateEnableStatus(@RequestBody PortPoolUpdateEnableStatusReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");
        ParamCheckUtil.checkNotNull(req.getEnable(), "enable");

        return portPoolService.updateEnableStatus(req);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody PortPoolDeleteReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");

        portPoolService.delete(req.getId());
    }

    @GetMapping("/get-available-port-list")
    public PageInfo<PortPoolListRes> getAvailablePortList(AvailablePortListReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getLicenseId(), "licenseId");
        return portPoolService.getAvailablePortList(req);
    }


    @GetMapping("/get-by-group")
    public List<PortPoolListRes> portListByGroupId(String groupId) {
        return portPoolService.portListByGroupId(groupId);
    }

    @PutMapping("/update-group")
    public PortPoolUpdateGroupRes updateGroup(@RequestBody PortPoolUpdateGroupReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getGroupId(), "groupId");
        ParamCheckUtil.checkNotEmpty(req.getPortIdList(), "portIdList");
        return portPoolService.updateGroup(req);
    }

    @PostMapping("/delete-batch")
    public void deleteBatch(@RequestBody PortPoolDeleteBatchReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getIds(), "ids");

        portPoolService.deleteBatch(req.getIds());
    }

    @GetMapping("/port-available")
    public boolean portAvailable(Integer port, String portMappingId) {
        ParamCheckUtil.checkNotNull(port, "port");

        return portPoolService.portAvailable(port, portMappingId);
    }
}
